<?php
// +----------------------------------------------------------------------
// | Project 项目服务
// +----------------------------------------------------------------------
// | 主要服务于Project数据处理
// +----------------------------------------------------------------------
// | 错误编码头 221xxx
// +----------------------------------------------------------------------

namespace common\service;

use common\model\ProjectRelationTemplateTodoModel;
use common\model\TemplateTodoModel;
use support\ErrorCode;
use support\SingletonTrait;


class ProjectRelationTemplateTodoService
{

    // 单例工具
    use SingletonTrait;

    /**
     * 设置项目待办模板成员
     * @param $data
     * @return array|bool
     * @throws \Exception
     */
    public function setMember($data)
    {
        $templateTodoId = $data['template_todo_id'];
        $templateTodoData = model(TemplateTodoModel::class)->where(['id' => $templateTodoId])->find();
        if (empty($templateTodoData)) {
            throw_strack_exception('', 'template todo not find', ErrorCode::TEMPLATE_TODO_NOT_FIND);
        }
        $projectId = $data['project_id'];
        $projectRelationTemplateData = model(ProjectRelationTemplateTodoModel::class)
            ->where(['template_todo_id' => $templateTodoData['id'], 'project_id' => $projectId])
            ->find();
        if (!empty($projectRelationTemplateData)) {
            $updateData = [
                'id' => $projectRelationTemplateData['id']
            ];
            $updateData = array_merge($updateData, $data);
            return model(ProjectRelationTemplateTodoModel::class)->modifyItem($updateData);
        }
        return model(ProjectRelationTemplateTodoModel::class)->addItem($data);
    }

}
